#!/bin/bash
# 获取脚本执行路径
SCRIPT_DIR=$(cd $(dirname $0); pwd)
cd $SCRIPT_DIR
INSTALL_DIR=/opt/software

yum -y install freeipmi

# 目录不存在则创建 /opt/software
if [ ! -d "$INSTALL_DIR" ]; then
    mkdir -p $INSTALL_DIR
fi

# 判断系统架构解压对应文件
if [ "$(uname -m)" == "x86_64" ]; then
    tar -zxvf files/ipmi_exporter_linux_amd64.tar.gz -C $INSTALL_DIR
else
    tar -zxvf files/ipmi_exporter_linux_arm64.tar.gz -C $INSTALL_DIR
fi

mv templates/ipmi_local.yml.j2 $INSTALL_DIR/ipmi_exporter/ipmi_local.yml

# 创建systemd服务文件
echo '[Unit]
Description=IPMI Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=root
Group=root
Type=simple
ExecStart=/opt/software/ipmi_exporter/ipmi_exporter --config.file=/opt/software/ipmi_exporter/ipmi_local.yml
Restart=on-failure
ExecStop=/bin/kill -SIGTERM $MAINPID
[Install]
WantedBy=multi-user.target' > /etc/systemd/system/ipmi_exporter.service

# enable ipmi_exporter in systemctl
systemctl daemon-reload
systemctl start ipmi_exporter
systemctl enable ipmi_exporter

echo "Setup complete. Add to Prometheus By Readme ."